package com.bdqn.jk.controller.admin;

import com.bdqn.jk.common.ServiceResultEnum;
import com.bdqn.jk.entity.AdminUser;
import com.bdqn.jk.service.AdminUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminUserService adminUserService;

    @GetMapping("/login")
    public String login(){
        return "admin/login";
    }

    @GetMapping("/test")
    public String test(){
        return "admin/test";
    }

    @GetMapping({"", "/", "/index", "/index.html"})
    public String index(HttpServletRequest request){
        request.setAttribute("path", "index");
        request.setAttribute("categoryCount", 0);
        request.setAttribute("blogCount", 0);
        request.setAttribute("linkCount", 0);
        request.setAttribute("tagCount", 0);
        request.setAttribute("commentCount", 0);
        request.setAttribute("path", "index");
        return "admin/index";
    }

    @PostMapping(value = "/login")
    public String login(@RequestParam("userName")String userName,
                        @RequestParam("password")String password, HttpSession session){
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "admin/login";
        }
        AdminUser adminUser = adminUserService.login(userName,password);
        if(adminUser != null ){
            session.setAttribute("loginUser",adminUser.getNickName());
            session.setAttribute("loginUserId",adminUser.getAdminUserId());
            return "admin/index";
        }else {
            session.setAttribute("errorMsg","登录失败！");
            return "admin/login";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("loginUserId");
        request.getSession().removeAttribute("loginUser");
        request.getSession().removeAttribute("errorMsg");
        return "admin/login";
    }

    @GetMapping("/profile")
    public String profile(HttpServletRequest request){
        Integer logUserId = (int)request.getSession().getAttribute("loginUserId");
        AdminUser adminUser = adminUserService.selectById(logUserId);
        if(adminUser!=null){
            return "admin/login";
        }
        request.setAttribute("path","profile");
        request.setAttribute("loginUserName",adminUser.getLoginUserName());
        request.setAttribute("nickName",adminUser.getNickName());
        return "admin/profile";
    }

    //根据ID修改密码
    @PostMapping("/profile/password")
    @ResponseBody
    public String updatePassword(HttpServletRequest request,@RequestParam("originalPassword")String originalPassword,
                                @RequestParam("newPassword") String newPassword){
        if (StringUtils.isEmpty("originalPassword")||StringUtils.isEmpty("newPassword")){
            return "密码不能为空！";
        }
        Integer loginUserId =(int) request.getSession().getAttribute("loginUserId");
        if (adminUserService.updatePassword(loginUserId,originalPassword,newPassword)){
            request.getSession().removeAttribute("loginUserId");
            request.getSession().removeAttribute("loginUser");
            request.getSession().removeAttribute("errorMsg");
            return ServiceResultEnum.SUCCESS.getResult();
        }else {
            return "修改失败！";
        }
    }

    //根据id修改用户名
    @PostMapping("/profile/name")
    @ResponseBody
    public String updateName(HttpServletRequest request,@RequestParam("loginUserName")String loginUserName,@RequestParam("nickName")String nickName){
        if (StringUtils.isEmpty(loginUserName)||StringUtils.isEmpty(nickName)){
            return "用户名不能为空！";
        }
        Integer logonUserId =(int) request.getSession().getAttribute("loginUserId");
        if(adminUserService.updateName(logonUserId,loginUserName,nickName)){
            return  ServiceResultEnum.SUCCESS.getResult();
        }else {
            return "修改失败！";
        }
    }
}
